<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\exhibition\Category;
use app\common\model\exhibition\Exhibition as ExhibitionExhibition;
use app\common\model\exhibition\Info;
use think\Validate;

/**
 * 展会接口
 */
class Exhibition extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (展会分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $list = Category::all();
        $this->success('请求成功', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (展会列表)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=false, sample="", description="分类")
     * @ApiParams (name="keywords", type="string", required=false, sample="", description="关键字")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $model = new ExhibitionExhibition();
        if (isset($id) && $id) {

            $model->where('category_id', $id);
        }
        if (isset($keywords) && $keywords) {
            $model->whereLike('title', '%' . $keywords . '%');
        }
        $lists = $model->where('status','normal')->paginate();
        $this->success('请求成功', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (展会详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $param = request()->param();
        extract($param);
        $data = ExhibitionExhibition::get($id);
        $this->success('请求成功', $data);
    }

    /**
     * @Author LK
     * @ApiTitle (展会报名)
     * @ApiMethod (POST)
     * @ApiParams (name="exhibition_id", type="integer", required=true, sample="", description="展会ID")
     * @ApiParams (name="name", type="string", required=true, sample="", description="姓名")
     * @ApiParams (name="mobile", type="string", required=true, sample="", description="手机号")
     * @ApiParams (name="landline", type="string", required=true, sample="", description="座机")
     * @ApiParams (name="title", type="string", required=false, sample="", description="单位名称")
     * @ApiParams (name="email", type="string", required=false, sample="", description="邮箱")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function sign_up()
    {
        $param = request()->param();
        extract($param);
        $validate = new Validate([
            'name'   => 'require',
            'mobile'  => 'require',
            'exhibition_id'  => 'require|gt:0',
        ], [
            'name.require'  => '姓名不能为空',
            'mobile.require' => '手机号不能为空',
            'exhibition_id' => '请选择报名展会',
        ]);
        $result = $validate->check($param);
        if (!$result) {
            $this->error(__($validate->getError()));
        }
        $row = ExhibitionExhibition::get($exhibition_id);
        $date = explode(' - ', $row['timerange']);
        $starttime =  strtotime($date[0]);
        $endtime = strtotime($date[1] . ' 23:59:59');
        if ($starttime > time() || $endtime < time()) {
            $this->error('未到或已超过报名时间');
        }
        if ($row->number >= $row->num) {
            $this->error('报名人数已达上限');
        }
        $param['user_id'] = $this->auth->id;
        $info = new Info($param);
        $res = $info->allowField(true)->save();
        if ($res) {
            $this->success('成功');
        }
        $this->error('失败');
    }
    /**
     * @Author LK
     * @ApiTitle (我报名的展会)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */

    public function my_exhibition()
    {
        $list = Info::with('exhibition')->where('user_id', $this->auth->id)->order('id desc')->paginate();
        $this->success('', $list);
    }
}
